package com.atguigu.xuexiplus.controller;

import com.atguigu.xuexiplus.dto.SubmitAnswerDTO;
import com.atguigu.xuexiplus.entity.Question;
import com.atguigu.xuexiplus.dto.SubmissionDetailsVO;
import com.atguigu.xuexiplus.result.Result;
import com.atguigu.xuexiplus.service.QuestionService;
import com.atguigu.xuexiplus.service.QuizService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    private final QuestionService questionService;
    private final QuizService quizService;

    public QuestionController(QuestionService questionService, QuizService quizService) {
        this.questionService = questionService;
        this.quizService = quizService;
    }

    /**
     * 获取随机题目
     * 示例：GET /api/questions/random?subject=CHINESE&gradeLevel=3&count=10
     */
    @GetMapping("/random")
    public Result<List<Question>> random(@RequestParam String subject,
                                         @RequestParam Integer gradeLevel,
                                         @RequestParam(required = false, defaultValue = "10") Integer count) {
        List<Question> list = questionService.getRandomQuestions(subject, gradeLevel, count);
        return Result.success(list);
    }

    /**
     * 提交答案并返回评分及每题对错
     */
    @PostMapping("/submit")
    public Result<Map<String, Object>> submit(@RequestBody SubmitAnswerDTO dto) {
        Map<String, Object> data = quizService.submit(dto);
        return Result.success(data);
    }

    /**
     * 提交详情：逐题对错与题面
     */
    @GetMapping("/submission/{submissionId}/details")
    public Result<SubmissionDetailsVO> details(@PathVariable Long submissionId) {
        SubmissionDetailsVO vo = quizService.getDetails(submissionId);
        if (vo == null) {
            return Result.error("submission not found");
        }
        return Result.success(vo);
    }
}



